from http import cookies
from http.client import responses

import requests

import re

from requests import session

# login_url = 'http://127.0.0.1:8000/admin/login/?next=/admin/login'
#
# response = requests.get(login_url)
# # print(response,response.text)
# # 先发起get请求获取隐藏域csrfmiddlewaretoken和cookie
# csrfmiddlewaretoken = re.search(r'<input type="hidden" name="csrfmiddlewaretoken" value="(.*?)">',response.text).group(1)
#
# print(csrfmiddlewaretoken)
#
#
# #  发起post请求 携带隐藏于 用户名 密码
#
# response = requests.post(login_url, data={
#     'csrfmiddlewaretoken': csrfmiddlewaretoken,
#     'username': 'admin',
#     'password': '123456',
# },cookies=response.cookies)
#
# print(response.text)


url = 'http://127.0.0.1:8000/admin/login/?next=/admin/login'
session = requests.Session()
print(session.cookies)
response = session.get(url)
print(session.cookies)

csrfmiddlewaretoken = re.search(r'<input type="hidden" name="csrfmiddlewaretoken" value="(.*?)">',response.text).group(1)

print(csrfmiddlewaretoken)

response = session.post(url,data={
    'csrfmiddlewaretoken': csrfmiddlewaretoken,
    'username': 'admin',
    'password': '123456',
})
print(response.text)




